import {
  isNode as D,
  isCollection as O,
  isScalar as K,
  isSeq as P,
} from '../nodes/identity.js';
import { Scalar as j } from '../nodes/Scalar.js';
import { stringify as C } from './stringify.js';
import { lineComment as F, indentComment as V } from './stringifyComment.js';
function z({ key: l, value: n }, e, c, h) {
  const {
    allNullValues: S,
    doc: W,
    indent: L,
    indentStep: E,
    options: { commentString: d, indentSeq: q, simpleKeys: a },
  } = e;
  let t = (D(l) && l.comment) || null;
  if (a) {
    if (t) throw new Error('With simple keys, key nodes cannot have comments');
    if (O(l)) {
      const r = 'With simple keys, collection cannot be used as a key value';
      throw new Error(r);
    }
  }
  let s =
    !a &&
    (!l ||
      (t && n == null && !e.inFlow) ||
      O(l) ||
      (K(l)
        ? l.type === j.BLOCK_FOLDED || l.type === j.BLOCK_LITERAL
        : typeof l == 'object'));
  e = Object.assign({}, e, {
    allNullValues: !1,
    implicitKey: !s && (a || !S),
    indent: L + E,
  });
  let g = !1,
    p = !1,
    i = C(
      l,
      e,
      () => (g = !0),
      () => (p = !0),
    );
  if (!s && !e.inFlow && i.length > 1024) {
    if (a)
      throw new Error(
        'With simple keys, single line scalar must not span more than 1024 characters',
      );
    s = !0;
  }
  if (e.inFlow) {
    if (S || n == null) return g && c && c(), i === '' ? '?' : s ? `? ${i}` : i;
  } else if ((S && !a) || (n == null && s))
    return (
      (i = `? ${i}`), t && !g ? (i += F(i, e.indent, d(t))) : p && h && h(), i
    );
  g && (t = null),
    s
      ? (t && (i += F(i, e.indent, d(t))),
        (i = `? ${i}
${L}:`))
      : ((i = `${i}:`), t && (i += F(i, e.indent, d(t))));
  let u, w, y;
  D(n)
    ? ((u = !!n.spaceBefore), (w = n.commentBefore), (y = n.comment))
    : ((u = !1),
      (w = null),
      (y = null),
      n && typeof n == 'object' && (n = W.createNode(n))),
    (e.implicitKey = !1),
    !s && !t && K(n) && (e.indentAtStart = i.length + 1),
    (p = !1),
    !q &&
      E.length >= 2 &&
      !e.inFlow &&
      !s &&
      P(n) &&
      !n.flow &&
      !n.tag &&
      !n.anchor &&
      (e.indent = e.indent.substring(2));
  let $ = !1;
  const f = C(
    n,
    e,
    () => ($ = !0),
    () => (p = !0),
  );
  let o = ' ';
  if (t || u || w) {
    if (
      ((o = u
        ? `
`
        : ''),
      w)
    ) {
      const r = d(w);
      o += `
${V(r, e.indent)}`;
    }
    f === '' && !e.inFlow
      ? o ===
          `
` &&
        (o = `

`)
      : (o += `
${e.indent}`);
  } else if (!s && O(n)) {
    const r = f[0],
      b = f.indexOf(`
`),
      N = b !== -1,
      A = e.inFlow ?? n.flow ?? n.items.length === 0;
    if (N || !A) {
      let B = !1;
      if (N && (r === '&' || r === '!')) {
        let m = f.indexOf(' ');
        r === '&' &&
          m !== -1 &&
          m < b &&
          f[m + 1] === '!' &&
          (m = f.indexOf(' ', m + 1)),
          (m === -1 || b < m) && (B = !0);
      }
      B ||
        (o = `
${e.indent}`);
    }
  } else
    (f === '' ||
      f[0] ===
        `
`) &&
      (o = '');
  return (
    (i += o + f),
    e.inFlow
      ? $ && c && c()
      : y && !$
        ? (i += F(i, e.indent, d(y)))
        : p && h && h(),
    i
  );
}
export { z as stringifyPair };
